import { inject, reactive } from 'vue'

const STORE_KEY = '__store__'
class Store {
    public _state
    public _mutation
    constructor(options: any) {
        this._state = reactive({
            data: options.state()
        })
        this._mutation = options.mutations
    }
    get state () {
        return this._state.data
    }
    commit(type, payload) {
        console.log('commit', type)
        const entry = this._mutation[type]
        console.log('entry', entry)
        entry && entry(this.state, payload)
    }
    install(app: any) {
        app.provide(STORE_KEY, this)
    }
}
const useStore = () => {
    return inject(STORE_KEY)
}
const createStore = (options: any) => {
 return new Store(options)
}

export default {
    createStore,
    useStore
}

